<?php
// 房东管理
namespace App\Http\Controllers\Admin;

use App\Exports\FangOwnerExport;
use App\Models\FangOwner;
use Illuminate\Http\Request;
use App\Http\Controllers\Admin\BaseController;
// excel类
use Maatwebsite\Excel\Facades\Excel;

class FangOwnerController extends BaseController
{
    /**
     * 列表
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        // 获取搜索框
        $kw = $request->get('kw');
        // 分页 搜索
        // 参数1 变量值存在，则执行 参数2 ---> 匿名函数
        $data = FangOwner::when($kw, function ($query) use ($kw) {
            $query->where('name', 'like', "%{$kw}%");
            $query->orWhere('phone', 'like', "%{$kw}%");
            $query->orWhere('card', 'like', "%{$kw}%");
            $query->orWhere('address', 'like', "%{$kw}%");
            $query->orWhere('email', 'like', "%{$kw}%");
        })->paginate($this->pagesize);
        return view('admin.fangowner.index', compact('data'));
    }

    /**
     * 添加显示
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.fangowner.create');
    }

    // 文件上传
    public function upfile(Request $request){
        // 默认图标
        $pic = config('up.pic');
        if($request->hasFile('file')){
            // 上传
            // 参数2 配置的节点名称
            $ret = $request->file('file')->store('','fangowner');
            $pic = '/uploads/fangowner/'.$ret;
        }
        return ['status' => 0,'url' => $pic];
    }

    // 文件删除
    public function delfile(Request $request){
        $filepath = $request->get('file');
        // 得到真实的地址
        $path = public_path().$filepath;
        // 删除指定文件
        unlink($path);
        return ['status'=>0,'msg'=>'success'];
    }

    /**
     * 添加房东处理
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        // 表单验证
        $this->validate($request,[
            'name' => 'required',
            'phone' => 'required'
        ]);
        // 获取数据
        $postData = $request->except(['_token','file']);
        // 去除第一个#
        $postData['pic'] = trim($postData['pic'],'#');
        // 入库
        FangOwner::create($postData);
        // 跳转到列表页面
        return redirect(route('admin.fangowner.index'));
    }

    /**
     * 显示图片
     *
     * @param  \App\Models\FangOwner  $fangOwner
     * @return \Illuminate\Http\Response
     */
    public function show(FangOwner $fangowner)
    {
        $picList = explode('#',$fangowner->pic);
        // 遍历
        array_map(function($item){
            echo "<div><img src=$item style='max-width: 780px;' /></div>";
        },$picList);

        return '';
    }

    // 导出excel
    public function exports(){
        return Excel::download(new FangOwnerExport(),'fangdong.xlsx');
    }

    /**
     * 修改房东
     *
     * @param  \App\Models\FangOwner  $fangowner
     * @return \Illuminate\Http\Response
     */
    public function edit(FangOwner $fangowner)
    {
        return view('admin.fangowner.edit',compact('fangowner'));
    }

    /**
     * 修改房东入库
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\FangOwner  $fangowner
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, FangOwner $fangowner)
    {
        // 表单验证
        $this->validate($request,[
            'name' => 'required',
            'phone' => 'required'
        ]);
        // 获取数据
        $postData = $request->except(['_token','file']);
        // 去除第一个#
        $postData['pic'] = trim($postData['pic'],'#');
        // 入库
        $fangowner->update($postData);
        // 跳转到列表页面
        return redirect(route('admin.fangowner.index'));
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\FangOwner  $fangOwner
     * @return \Illuminate\Http\Response
     */
    public function destroy(FangOwner $fangowner)
    {
        $fangowner->delete();
        return ['status' => 0,'msg' => '删除成功'];
    }
}
